<template>
	<view class="login u-skeleton">
		<view class="logo">
			<image class="userinfo-avatar u-skeleton-circle" src="../../static/logo.png"></image>
		</view>
		<view class="input">
			<input placeholder="请输入手机号" v-model="from.username" />
		</view>
		<view class="input" v-if="type == 'possword'">
			<input placeholder="请输入密码" v-model="from.password" type="password"/>
			<!-- <view class="txt-possword" @click.stop="$util.redirectTo('/pages/login/forget')">忘记密码</view> -->
		</view>
		<view class="input input-code" v-else>
			<input placeholder="请输入验证码" v-model="from.verification_code" />
			<text class="txt" @click="getcode" v-if="!time_num">获取验证码</text>
			<text class="txt code" v-else>{{time_num}}s</text>
		</view>
		<u-button class="primary" @click="btn" ripple-bg-color="rgba(0,0,0,.4)" :ripple="true">登录</u-button>
		<view class="tips-account" @click="$util.redirectTo('/pages/login/register')">没有账号？<text
				class="tips—txt">立即注册</text></view>
		<view class="select-login-type">
			<image :src="$util.img('/static/img/phone.png')" mode="widthFix" @click="type = ''"></image>
			<image :src="$util.img('/static/img/possword.png')" mode="widthFix" @click="type = 'possword'"></image>
		</view>
		<view class="die-tips">
			<u-checkbox @change="checkboxChange" v-model="checked" width="120" shape="circle" active-color="#000"></u-checkbox>
			<text>我已阅读并同意</text>
			<text class="info" @click="gotolink('user')">《用户协议》</text>
			<text>和</text>
			<text class="info" @click="gotolink('policy')">《隐私政策》</text>-
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userInfo: {
					avatarUrl: 'https://qlogo2.store.qq.com/qzone/1416956117/1416956117/100?1531323520',
					nickName: 'uView'
				},
				
				code: '',
				time_num: 0,
				checked: false,
				//判断什么登录
				type: 'possword',
				// 需要用到的信息
				from:{
					password: '',
					username:'',
					verification_code:'',
				},
				logo:''
			}
		},
		onLoad(option) {
			if(option.code) uni.setStorageSync('code',option.code)
			//this.type = option.type;
			this.getinfo();
		},
		methods: {
			btn() {
				if(!this.checked){
					this.$util.taost({
						title:'请先同意协议'
					})
					return false
				}
				if(!this.from.username){
					this.$util.taost({
						title:'请填写手机号'
					})
					return false
				}
				if(this.type == 'possword'){
					if(!this.from.password){
						this.$util.taost({
							title:'请填写密码'
						})
						return false
					}
				} else {
					if(!this.from.verification_code){
						this.$util.taost({
							title:'请填写验证码'
						})
						return false
					}
				}

				this.$http.asyncSendrequest({
					url:'/login/login',
					data:{
						type:this.type == 'possword' ? 2 : 1,
						...this.from
					},
					success:res=>{
						if(res.code == '0') {
							uni.setStorageSync('token',res.data)
							setTimeout(()=>{
								this.$util.redirectTo('/pages/index/index',{},'switchTab');
							},1000)
						}
						this.$util.taost({
							title:res.msg
						})
					}
				})
				
			},
			gotolink(type){
				this.$util.redirectTo('/pages/login/agreement',{type});
			},
			getcode() {
				if(!this.from.username){
					this.$util.taost({
						title:'请输入手机号'
					})
					return false
				}
				this.$http.asyncSendrequest({
					url:'/login/sms_verification_code',
					data:{
						sms_type:2,
						mobile:this.from.username,
						type:'login'
					},
					success:res=>{
						if(res.code == 1){
							this.time_num = 60;
							const countdown = setInterval(()=>{
								this.time_num -= 1;
								if(!this.time_num) clearTimeout(countdown);
							},1000)
						} 
						this.$util.taost({
							title:res.msg
						})
					}
				})
			},
			checkboxChange(e) {
				// console.log("数据",e);
			},
			//获取基础数据
			getinfo(){
				this.$http.asyncSendrequest({
					url:'/common/baseConfig',
					success:res=>{
						this.logo = res.data.app_image;
					}
				})
			}
		}

	}
</script>

<style lang="scss" scoped>
	.login {
		width: 100%;
		max-height: 100vh;
		padding-top: 190rpx;
		box-sizing: border-box;

		.logo {
			margin: 0 auto 134rpx;
			width: 180rpx;
			height: 180rpx;
			// background: #292929;
			border-radius: 20rpx;
			image{
				width: 100%;
				height: 100%;
			}
		}

		.input {
			margin: 0 40rpx 24rpx;
			padding: 0 35rpx;
			box-sizing: border-box;
			height: 100rpx;
			border: 1rpx solid #292929;
			border-radius: 10rpx;
			display: flex;
			align-items: center;
			position: relative;

			input {
				width: calc(100% - 200rpx);
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
			}

		}

		.input-code {
			justify-content: space-between;

			.txt {
				font-size: 28rpx;
				font-family: PingFang SC;
				font-weight: 500;
				color: #000000;
				text-align: right;
			}
			
			.code {
				color: rgba(0, 0, 0, .4);
				
			}

		}

		.txt-possword {
			position: absolute;
			right: 0;
			bottom: -52rpx;
		}

		.primary {
			height: 100rpx;
			background: #292929;
			border-radius: 100rpx;
			margin: 64rpx 40rpx 0;
			color: #fff;
			font-size: 34rpx;
		}

		.tips-account {
			text-align: center;
			margin-top: 44rpx;
			font-size: 28rpx;

			.tips—txt {
				color: #397EF1;
			}
		}

		.select-login-type {
			display: flex;
			align-items: center;
			justify-content: center;
			margin-top: 80rpx;

			image {
				width: 100rpx;
				margin: 0 20rpx;
			}
		}

		.die-tips {
			display: flex;
			align-items: center;
			justify-content: center;
			font-size: 28rpx;
			margin-top: 100rpx;
			color: #999999;

			.info {
				color: #000000;
			}
		}

		/deep/ .u-checkbox {
			width: 48rpx !important;
		}
	}
</style>